package com.jilian.mydemo.modul.order.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.PathDto;
import com.jilian.mydemo.listener.CustomItemClickListener;

import java.util.List;


public class SignUpAdapter extends RecyclerView.Adapter<SignUpAdapter.ViewHolder> {
    private Activity mContext;
    private List<PathDto> datas;
    private CustomItemClickListener listener;
    private CloseListener closeListener;
    public interface  CloseListener{
        void close(int position);
    }

    public SignUpAdapter(Activity context, List<PathDto> datas, CustomItemClickListener listener,CloseListener  closeListener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
        this.closeListener =closeListener;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_sign_up, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //添加按钮
        if(datas.get(position).getType()==1){
            holder.ivHead.setImageResource(R.drawable.image_add_photo);
            holder.ivCloseOne.setVisibility(View.GONE);
        }
        else{
            //需要显示的图片
            holder.ivCloseOne.setVisibility(View.VISIBLE);
            Glide.with(mContext).load(datas.get(position).getUrl()).into(holder.ivHead);

        }
        holder.ivCloseOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeListener.close(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivHead;
        private ImageView ivCloseOne;

        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            ivCloseOne = (ImageView)itemView. findViewById(R.id.iv_close_one);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView, getAdapterPosition());
                }
            });


        }

    }
}
